package v1

import (
	"log"
	"net/http"
	"toy-car/models"
	"toy-car/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

// @Tags SysDictionary
// @Summary 创建SysDictionary
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionary true "SysDictionary模型"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /sysDictionary/createSysDictionary [post]
func DictionaryAdd(c *gin.Context) {
	var dictionary models.SysDictionary
	_ = c.ShouldBindJSON(&dictionary)

	code := e.SUCCESS
	msg := e.GetMSG(code)

	err := models.CreateSysDictionary(dictionary)
	log.Printf("%v---->", err)
	if err != nil {
		code = e.ERROR
		msg = err.Error()
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  msg,
		"data": true,
	})
}

// @Tags SysDictionary
// @Summary 删除SysDictionary
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionary true "SysDictionary模型"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /sysDictionary/deleteSysDictionary [delete]
func DictionaryDelete(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	var dictionary models.SysDictionary
	_ = c.ShouldBindJSON(&dictionary)
	dictionary.ID = id
	code := e.SUCCESS
	err := models.DeleteSysDictionary(dictionary)
	if err != nil {
		code = e.ERROR
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": true,
	})
}

// @Tags SysDictionary
// @Summary 更新SysDictionary
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionary true "SysDictionary模型"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /sysDictionary/updateSysDictionary [put]
func DictionaryUpdate(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	var dictionary models.SysDictionary
	_ = c.ShouldBindJSON(&dictionary)

	dictionary.ID = id
	code := e.SUCCESS
	err := models.UpdateSysDictionary(&dictionary)
	if err != nil {
		code = e.ERROR
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": true,
	})
}

// @Tags SysDictionary
// @Summary 用id查询SysDictionary
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionary true "ID或字典英名"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /sysDictionary/findSysDictionary [get]
func DictionaryGetByID(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	// var dictionary models.SysDictionary
	// _ = c.ShouldBindQuery(&dictionary)

	code := e.SUCCESS
	msg := e.GetMSG(code)
	err, sysDictionary := models.GetSysDictionary("", uint(id))
	if err != nil {
		code = e.ERROR
		msg = err.Error()
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  msg,
		"data": sysDictionary,
	})
}

// @Tags SysDictionary
// @Summary 用id查询SysDictionary
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDictionary true "ID或字典英名"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /sysDictionary/findSysDictionary [get]
func DictionaryGetList(c *gin.Context) {

	var pageInfo models.SysDictionarySearch
	_ = c.ShouldBindQuery(&pageInfo)

	data := make(map[string]interface{})
	code := e.SUCCESS

	err, list, total := models.GetSysDictionaryInfoList(pageInfo)

	if err != nil {
		code = e.ERROR
	}
	data["data"] = list
	data["total"] = total
	data["page"] = pageInfo.Page
	data["pageSize"] = pageInfo.PageSize
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}
